<?php
namespace zake\foundation\reactor;

use zake\foundation\able\reactor\LoopAdaptAble;
use zake\foundation\able\stream\ReadAble;
use zake\foundation\able\stream\RwBufferAble;
use zake\foundation\able\stream\WriteAble;

class BaseLoop
{
    protected $_readMap     = [];
    protected $_writeMap    = [];

    /**
     * @var null | LoopAdaptAble
     */
    protected $_adapt = null;


    /**
     * Loop constructor.
     * @param LoopAdaptAble $adapt
     */
    protected function __construct(LoopAdaptAble $adapt)
    {
        if($adapt instanceof LoopAdaptAble){
            $this->_adapt = $adapt;
        }
    }


    /**
     *
     * 循环运行
     * @author liu.bin 2020-04-15 18:05
     */
    public function loop()
    {
        $this->_adapt->loop();
    }


    /**
     *
     * @param ReadAble $stream
     * @param \Closure $callBack
     *
     * @author liu.bin 2020-04-19 15:40
     */
    public function addRead(ReadAble $stream,\Closure $handler)
    {
        $fd         = $stream->getReadFd();
        $resource   = $stream->getReadResource();
        $this->_readMap[$fd] = $stream;
        $this->_adapt->addRead($resource,$handler);
    }


    /**
     *
     * @param WriteAble $stream
     * @param \Closure $callBack
     *
     * @author liu.bin 2020-04-19 15:41
     */
    public function addWrite(WriteAble $stream,\Closure $handler)
    {
        $fd         = $stream->getWriteFd();
        $resource   = $stream->getWriteResource();
        $this->_writeMap[$fd] = $stream;
        $this->_adapt->addWrite($resource,$handler);
    }


    /**
     * 添加定时器
     * @param $interval
     * @param \Closure $handler
     * @return mixed
     *
     * @author liu.bin 2020-04-27 22:53
     */
    public function addTime($interval,\Closure $handler,$param=[])
    {
        return $this->_adapt->addTime($interval,$handler,$param);
    }


    public function addSignal($sigNo,\Closure $handler)
    {
        $this->_adapt->addSignal($sigNo,$handler);
    }


    /**
     *
     * @param ReadAble $stream
     *
     * @author liu.bin 2020-04-19 15:43
     */
    public function removeRead($stream)
    {
        $fd = $stream->getReadFd();
        unset($this->_readMap[$fd]);
        $this->_adapt->removeRead($fd);
    }


    /**
     *
     * @param WriteAble $stream
     *
     * @author liu.bin 2020-04-19 15:44
     */
    public function removeWrite($stream)
    {
        $fd = $stream->getWriteFd();
        unset($this->_writeMap[$fd]);
        $this->_adapt->removeWrite($fd);
    }



    public function removeTime($timeId)
    {
        $this->_adapt->removeTime($timeId);
    }



    public function removeSignal($sigNo)
    {
        $this->_adapt->removeSignal($sigNo);
    }



    /**
     * 获取读stream
     * @param int $fd
     * @return RwBufferAble
     * @author liu.bin 2020-04-30 17:16
     */
    public function getReadStream($fd)
    {
        return $this->_readMap[$fd]??null;
    }


    /**
     * 获取所有读stream
     * @return array
     *
     * @author liu.bin 2020-05-07 13:11
     */
    public function getReadStreamMap()
    {
        return $this->_readMap;
    }

}